package com.apes.fn.crm.ReturnVisit.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.fn.crm.ReturnVisit.model.VisitHistory;
import com.apes.fn.crm.ReturnVisit.repository.VisitHistoryRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.framework.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service("visitHistoryService")
public class VisitHistoryService extends DomainService {

    @Autowired
    VisitHistoryRepository visitHistoryRepository;

    /***
     * 创建
     */

    public VisitHistory create(SimpleRequest request) {
        JSONObject tempJson = request.getJO();
        VisitHistory visitHistory = Tools.map(
                MapUtil.mapper(
                        "returnVisit", MapUtil.mapper("id", request.get("id")),
                        "state", tempJson.getJSONObject("visitState").getString("id"),
                        "visitSatisfaction", tempJson.get("visitSatisfaction"),
                        "visitDissatisfactionReason", tempJson.get("visitDissatisfactionReason"),
                        "visitFailReason", tempJson.get("visitFailReason"),
                        "advice", tempJson.get("advice"),
                        "remark", tempJson.get("remark"),
                        "notVisitSign", tempJson.get("notVisitSign"),
                        "visitPeriod", tempJson.get("visitPeriod"),
                        "startVisitTime", tempJson.get("startVisitTime"),
                        "endVisitTime", new Date(),
                        "nextVisitDate",  tempJson.get("nextVisitDate"),
                        "visitedId", request.getPerson(),
                        "visitedDate", new Date()
                ), VisitHistory.class);
        return visitHistoryRepository.saveAndFlush(visitHistory);
    }

    /***
     * 修改
     */
    public VisitHistory update(SimpleRequest request) {
        isValid(request, visitHistoryRepository);
        VisitHistory visitHistory = request.getO(VisitHistory.class);
        return visitHistoryRepository.saveAndFlush(visitHistory);
    }

    /***
     * 删除
     */
    public String delete(SimpleRequest request) {
        isValid(request, visitHistoryRepository);
        VisitHistory visitHistory = request.getO(VisitHistory.class);
        visitHistoryRepository.delete(visitHistory);
        return "删除成功！";
    }



    /***
     * 数据是否合法
     * @param request
     * @param repository
     */
    private Model isValid(SimpleRequest request, CustomRepository repository) {
        Model oldModel = (Model) repository.findOne((String) request.get("id"));
        if (oldModel == null) throw new RuntimeException("编码【" + request.get("id") + "】在系统中不存在。");
        if (oldModel.getVersion() != (int) request.get("version")) throw new RuntimeException("记录已更新或已删除，请重新提交！");
        return oldModel;
    }
}
